/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.utils;

import java.text.DecimalFormat;

public class Converter {

	private static final int microAmountRate = 1000000;
	
	
	/*
	 * Convert to microamount
	 */
	public static String strToMicroamount(String amount){
		String result = "0";
		try {
			double doubleAmount = 0;
			long longAmount = 0;
			if(amount != null){
				doubleAmount = Double.parseDouble(amount);
				doubleAmount = doubleAmount * microAmountRate;
				longAmount = (long) doubleAmount;
			}
			result = Long.toString(longAmount); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}//strToMicroamount
	
	
	
	/*
	 * Convert from microamount
	 */
	public static String microamountToStr(String microamount){
		String result = "";
		try {
			double doubleAmount = 0;
			if(microamount != null){
				doubleAmount = Double.parseDouble(microamount);
				doubleAmount = doubleAmount / microAmountRate;
			}
			
	        DecimalFormat df = new DecimalFormat("0.00");
			result = df.format(doubleAmount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;		
	}
	
	
	
	/*
	 * Format string number
	 */
	public static String formatStrNumber(String number){
		String result = "";
		try {
			double numberToFormat = 0;
			if(number != null){
				numberToFormat = Double.parseDouble(number);
			}
			
	        DecimalFormat df = new DecimalFormat("0.00");
			result = df.format(numberToFormat);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;		
	}//formatNumber
	
	
	/*
	 * Apply percent
	 */
	public static String formatStrNumberPercent(String number){
		String result = "";
		
		try {
			double numberToFormat = 0;
			if(number != null){
				numberToFormat = Double.parseDouble(number);
				numberToFormat = numberToFormat * 100;
			}
			
	        DecimalFormat df = new DecimalFormat("0.00");
			result = df.format(numberToFormat);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}//applyPercent
	
	
	
	/*
	 * Format double number
	 */
	public static String formatDouble(double number){
		String result = "";
		try {
	        DecimalFormat df = new DecimalFormat("0.00");
			result = df.format(number);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;			
	}
	
	/*
	 * Escape URI character (for WebView)
	 */
    public final static String escapeURI(String html) {
    	if(html == null)
    		return null;
    	StringBuilder buf = new StringBuilder(html.length());
        for (char c : html.toCharArray()) {
            switch (c) {
              case '#':  buf.append("%23"); break;
              case '%':  buf.append("%25"); break;
              case '\'': buf.append("%27"); break;
              case '?':  buf.append("%3f"); break;                
              default:
                buf.append(c);
                break;
            }
        }
        return buf.toString(); 
    }//escapeURI
	
	
    
    /*
     * Escape XML characters
     */
    public final static String escapeXML(String string){
        StringBuilder buf = new StringBuilder(string.length());
        for (char c : string.toCharArray()) {
            switch (c) {
              case '&':  buf.append("&amp;"); break;
              case '<':  buf.append("&lt;"); break;
              case '>':  buf.append("&gt;"); break;
              case (char)27:  buf.append("&apos;"); break; //'
              case '"':  buf.append("&qpos;"); break;
              default:
                buf.append(c);
                break;
            }
        }
        return buf.toString(); 
    }//escapeXML  
    
}
